package com.avcdata.national.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.national.module.ModelListViewInfo;
import com.avcdata.national.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * NewAOSApp
 * com.avcdata.national.adapter
 * Created by lic on 2017/9/20.
 * CSDN http://blog.csdn.net/lucasey
 */

public class ModelAdapter extends BaseAdapter {

    private List<ModelListViewInfo> list;
    private Context context;
    private boolean isNew = false;


    public ModelAdapter(List<ModelListViewInfo> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void setIsNew(boolean isNew){
        this.isNew = isNew;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        viewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_model, null);
            holder = new viewHolder();
            holder.spec1 = (TextView) convertView.findViewById(R.id.spec1);
            holder.spec2 = (TextView) convertView.findViewById(R.id.spec2);
            holder.spec3 = (TextView) convertView.findViewById(R.id.spec3);
            holder.spec4 = (TextView) convertView.findViewById(R.id.spec4);
            holder.spec5 = (TextView) convertView.findViewById(R.id.spec5);
            holder.tips1 = (TextView) convertView.findViewById(R.id.tips1);
            holder.tips2 = (TextView) convertView.findViewById(R.id.tips2);
            holder.rank = (TextView) convertView.findViewById(R.id.rank);
            holder.brand = (TextView) convertView.findViewById(R.id.brand);
            holder.model = (TextView) convertView.findViewById(R.id.model);
            holder.market_share = (TextView) convertView.findViewById(R.id.market_share);
            holder.avger_price = (TextView) convertView.findViewById(R.id.avger_price);
            holder.basicRatio = (TextView) convertView.findViewById(R.id.basicRatio);
            holder.Ratio = (TextView) convertView.findViewById(R.id.Ratio);
            holder.ratio_icon = (ImageView) convertView.findViewById(R.id.Ratio_icon);
            holder.basicRatio_icon = (ImageView) convertView.findViewById(R.id.basicRatio_icon);
            convertView.setTag(holder);
        }else{
            holder = (viewHolder) convertView.getTag();
        }

        List<TextView> list_tv = new ArrayList<>();
        list_tv.add(holder.spec1);
        list_tv.add(holder.spec2);
        list_tv.add(holder.spec3);
        list_tv.add(holder.spec4);
        list_tv.add(holder.spec5);
        for(TextView tv:list_tv){
            tv.setVisibility(View.GONE);
        }

        if(list.get(i).getBrand().equals("A.O.史密斯")){
            holder.rank.setTextColor(context.getResources().getColor(R.color.red));
            holder.brand.setTextColor(context.getResources().getColor(R.color.red));
            holder.model.setTextColor(context.getResources().getColor(R.color.red));
            holder.market_share.setTextColor(context.getResources().getColor(R.color.red));
            holder.avger_price.setTextColor(context.getResources().getColor(R.color.red));
            holder.basicRatio.setTextColor(context.getResources().getColor(R.color.red));
        }else{
            holder.rank.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.brand.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.model.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.market_share.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.avger_price.setTextColor(context.getResources().getColor(R.color.text_one));
        }

        holder.rank.setText(i+1+"");
        holder.brand.setText(list.get(i).getBrand());
        holder.model.setText(list.get(i).getModel());
        try {
            holder.market_share.setText(MyUtils.format2(Math.abs(Double.parseDouble(list.get(i).getMarketShare())))+"%");
        }catch (Exception e){
            holder.market_share.setText("--");
        }

        try {
            holder.basicRatio.setText(MyUtils.format2(Math.abs(Double.parseDouble(list.get(i).getMarketShareLinkRelativeRatio())))+"%");
            if(Double.parseDouble(list.get(i).getMarketShareLinkRelativeRatio())<0){
                holder.basicRatio.setTextColor(context.getResources().getColor(R.color.green));
//                holder.basicRatio_icon.setBackgroundResource(R.mipmap.data_decline);
            }else{
                holder.basicRatio.setTextColor(context.getResources().getColor(R.color.red));
//                holder.basicRatio_icon.setBackgroundResource(R.mipmap.data_rise);
            }
        }catch (Exception e){
            holder.basicRatio.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.basicRatio.setText("--");
        }

        try {
            holder.Ratio.setText(MyUtils.format2(Math.abs(Double.parseDouble(list.get(i).getAvgPriceRatio())))+"");
            if(Double.parseDouble(list.get(i).getAvgPriceRatio())<0){
//                holder.ratio_icon.setBackgroundResource(R.mipmap.data_decline);
                holder.Ratio.setTextColor(context.getResources().getColor(R.color.green));
            }else{
//                holder.ratio_icon.setBackgroundResource(R.mipmap.data_rise);
                holder.Ratio.setTextColor(context.getResources().getColor(R.color.red));
            }
        }catch (Exception e){
            holder.Ratio.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.Ratio.setText("--");
        }


        holder.avger_price.setText(list.get(i).getAvgPrice()+"");

        if(isNew){
            holder.basicRatio.setVisibility(View.GONE);
            holder.basicRatio_icon.setVisibility(View.GONE);
            holder.ratio_icon.setVisibility(View.GONE);
            holder.Ratio.setVisibility(View.GONE);
            holder.tips1.setVisibility(View.GONE);
            holder.tips2.setVisibility(View.GONE);
        }else{
            holder.basicRatio.setVisibility(View.VISIBLE);
            holder.basicRatio_icon.setVisibility(View.GONE);
            holder.ratio_icon.setVisibility(View.GONE);
            holder.Ratio.setVisibility(View.VISIBLE);
            holder.tips1.setVisibility(View.VISIBLE);
            holder.tips2.setVisibility(View.VISIBLE);
        }

        if(null==list.get(i).getSpec() || "".equals(list.get(i).getSpec())){

        }else{
            if(list.get(i).getSpec().contains(",")){
                for(int m=0;m<list.get(i).getSpec().split(",").length;m++){
                    list_tv.get(m).setVisibility(View.VISIBLE);
                    list_tv.get(m).setText(list.get(i).getSpec().split(",")[m]);
                    if(list.get(i).getBrand().equals("A.O.史密斯")){
                        list_tv.get(m).setTextColor(context.getResources().getColor(R.color.red));
                        list_tv.get(m).setBackgroundResource(R.drawable.bg_nocontent_redcirclestroke);
                    }else{
                        list_tv.get(m).setTextColor(context.getResources().getColor(R.color.text_one));
                        list_tv.get(m).setBackgroundResource(R.drawable.bg_nocontent_graycirclestroke);
                    }
                }
            }else{
                holder.spec1.setText(list.get(i).getSpec());
                holder.spec1.setVisibility(View.VISIBLE);
                if(list.get(i).getBrand().equals("A.O.史密斯")){
                    holder.spec1.setTextColor(context.getResources().getColor(R.color.red));
                    holder.spec1.setBackgroundResource(R.drawable.bg_nocontent_redcirclestroke);
                }else{
                    holder.spec1.setTextColor(context.getResources().getColor(R.color.text_one));
                    holder.spec1.setBackgroundResource(R.drawable.bg_nocontent_graycirclestroke);
                }
            }
        }

        return convertView;
    }

    private class viewHolder{
        private TextView spec1;
        private TextView spec2;
        private TextView spec3;
        private TextView spec4;
        private TextView spec5;
        private TextView tips1;
        private TextView tips2;
        private TextView rank;
        private TextView brand;
        private TextView model;
        private TextView market_share;
        private TextView avger_price;
        private TextView basicRatio;
        private TextView Ratio;
        private ImageView ratio_icon;
        private ImageView basicRatio_icon;
    }

}
